package com.zhx.business.controller;

import com.zhx.business.base.BaseController;
import com.zhx.business.entity.Product;
import com.zhx.business.repository.dto.ProductCondition;
import com.zhx.business.repository.spec.ProductSpec;
import com.zhx.business.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/v1/product")
@Api(tags = "product",description = "产品相关")
public class ProductController extends BaseController<Product> {

    @Autowired
    private ProductService productService;

    @PostMapping("/saveorupdate")
    @ApiOperation(value = "保存或者更新",notes = "保存或者更新")
    public ResponseEntity saveorupdate(@RequestBody Product product){
        Product save = productService.save(product);

        return ResponseEntity.ok(save);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取产品",notes = "根据id获取产品")
    public ResponseEntity getById(@PathVariable String id){
        Assert.hasText(id,"id不能为空");

        Product one = productService.findOne(id);

        return ResponseEntity.ok(one);
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取列表",notes = "获取列表")
    public ResponseEntity getList(@RequestBody ProductCondition condition,Pageable pageable){
        Page<Product> all = productService.findAll(new ProductSpec(condition), pageable);

        return ResponseEntity.ok(all);
    }

}
